package com.fulei.logic;

import android.util.Log;

import com.bjfl.saleManager.util.LogUtil;
import com.dwin.navy.serialportapi.SerailPortOpt;

public class FuleiFightControl
{
  private static SerailPortOpt serialPort = null;
  public static String comNo = "";
  public static String baudRate = "";
  
  public static boolean OpenPort()
  {
    try
    {
      serialPort = new SerailPortOpt();
      
      serialPort.mDevNum = Integer.parseInt(comNo);
      serialPort.mSpeed = Integer.parseInt(baudRate);
      serialPort.mDataBits = 8;
      serialPort.mStopBits = 1;
      serialPort.mParity = 110;
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
    return true;
  }
  
  public static void closeSerialPort()
  {
    try
    {
      if (serialPort.mFd != null) {
        serialPort.closeDev(serialPort.mFd);
      }
      serialPort.mFd = null;
      serialPort = null;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
  }
  
  public static int ResetKey()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 126, 0, 5, 1, 18, 0, 0, 1, 20, -19 };
      
      LogUtil.writeKeyLog("ResetKeySend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeKeyLog("ResetKeyRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] == 126) && (ReadData[9] == -19))
      {
        if ((ReadData[4] == -110) && (ReadData[7] == 1)) {
          return_data = 1;
        }
      }
      else {
        return -1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("复位异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int FlashKey(int flashNo, int flashMode, int flashSpeed)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[11];
      SendData[0] = 126;
      SendData[1] = 0;
      SendData[2] = 6;
      SendData[3] = 1;
      SendData[4] = 6;
      SendData[5] = 0;
      SendData[6] = ((byte)flashNo);
      SendData[7] = ((byte)flashMode);
      SendData[8] = ((byte)flashSpeed);
      SendData[9] = SumByte(SendData, 3, 5);
      SendData[10] = -19;
      
      LogUtil.writeKeyLog("FlashKeySend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeKeyLog("FlashKeyRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] == 126) && (ReadData[10] == -19))
      {
        if (ReadData[4] == -122) {
          return_data = 1;
        }
      }
      else {
        return -1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("闪灯控制接口异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int CutPaperFlashKey()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 126, 0, 5, 1, 2, 0, 1, 0, 4, -19 };
      LogUtil.writeKeyLog("CutPaperFlashKeySend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 10);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeKeyLog("CutPaperFlashKeyRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] == 126) && (ReadData[9] == -19))
      {
        if ((ReadData[4] == -126) && (ReadData[7] == 1)) {
          return_data = 1;
        }
      }
      else {
        return -1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(" 出票口闪灯异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int[] QuerySensorStatus()
  {
    int[] return_data = new int[2];
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 126, 0, 5, 1, 5, 0, 0, 0, 6, -19 };
      LogUtil.writeKeyLog("QuerySensorStatusSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return null;
      }
      LogUtil.writeKeyLog("QuerySensorStatusRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if (ReadData[0] == 126) {
        if (ReadData[4] == -123)
        {
          byte[] bitArr = getBooleanArray(ReadData[7]);
          if (bitArr[7] == 0) {
            return_data[0] = 0;
          } else if (bitArr[7] == 1) {
            return_data[0] = 1;
          }
          if (bitArr[6] == 0) {
            return_data[1] = 0;
          } else if (bitArr[6] == 1) {
            return_data[1] = 1;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("传感器和震动报警异常:" + ex.getMessage());
      return_data = null;
    }
    return return_data;
  }
  
  public static int[] QueryControlStatus()
  {
    int[] return_data = new int[4];
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 126, 0, 5, 1, 3, 0, 0, 0, 4, -19 };
      LogUtil.writeKeyLog("QueryControlStatusSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 10);
      if (ReadDataLength < 1) {
        return null;
      }
      LogUtil.writeKeyLog("QueryControlStatusRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126) {
        if (ReadData[4] == -125)
        {
          if ((0x1 & ReadData[6]) > 0) {
            return_data[0] = -2;
          } else if ((0x1 & ReadData[6]) <= 0) {
            return_data[0] = -3;
          }
          if ((0x2 & ReadData[6]) > 0) {
            return_data[1] = -4;
          } else if ((0x2 & ReadData[6]) <= 0) {
            return_data[1] = -5;
          }
          if ((0x8 & ReadData[6]) > 0) {
            return_data[2] = -6;
          } else if ((0x8 & ReadData[6]) <= 0) {
            return_data[2] = -7;
          }
          if (ReadData[7] == 0) {
            return_data[3] = -8;
          } else if (ReadData[7] == 1) {
            return_data[3] = -9;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("传感器和震动报警异常:" + ex.getMessage());
      return_data = null;
    }
    return return_data;
  }
  
  public static int fightFickerControl(byte[] speed, byte colors)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = sendFightControlData(speed, colors);
      LogUtil.writeKeyLog("fightFickerControl:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 10);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeKeyLog("fightFickerControl:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] == 126) && (ReadData[9] == -19))
      {
        if ((ReadData[2] == 5) && (ReadData[4] == 132)) {
          return_data = 1;
        }
      }
      else {
        return -1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(" 灯带闪灯异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static byte SumByte(byte[] data, int offset, int length)
  {
    int Result = 0;
    for (int i = offset; i <= offset + length; i++) {
      Result += data[i];
    }
    return (byte)Result;
  }
  
  public static byte[] getBooleanArray(byte b)
  {
    byte[] array = new byte[8];
    for (int i = 7; i >= 0; i--)
    {
      array[i] = ((byte)(b & 0x1));
      b = (byte)(b >> 1);
    }
    return array;
  }
  
  public static byte[] sendFightControlData(byte[] speed, byte colors)
  {
    byte[] SendCmd = new byte[10];
    SendCmd[0] = 126;
    SendCmd[1] = 0;
    SendCmd[2] = 5;
    SendCmd[3] = 1;
    SendCmd[4] = 4;
    SendCmd[5] = 0;
    SendCmd[6] = speed[0];
    SendCmd[7] = speed[1];
    SendCmd[8] = colors;
    SendCmd[9] = ((byte)(SendCmd[3] + SendCmd[4] + SendCmd[5] + SendCmd[6] + SendCmd[7] + SendCmd[8]));
    SendCmd[10] = -19;
    for (int i = 0; i < 11; i++) {
      Log.v("====>", "==>" + SendCmd[i]);
    }
    return SendCmd;
  }
}
